package com.wh.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wh.common.dao.TaskEmployeeMapper;
import com.wh.common.dao.TaskMapper;
import com.wh.common.dto.TaskQueryDto;
import com.wh.common.pojo.Task;
import com.wh.common.service.ITaskService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements ITaskService {

    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private TaskEmployeeMapper taskEmployeeMapper;


    @Override
    public IPage<Task> findByPage(TaskQueryDto taskQueryDto){
        //1.创建查询条件
        QueryWrapper<Task> qw = new QueryWrapper<>();
        //qw.like("xxx",taskQueryDto.getXxx());
        //qw.or();
        //qw.like("xxx",taskQueryDto.getXxx());
        //2.组件分页数据
        IPage<Task> page = new Page<>(taskQueryDto.getCurrentPage(), taskQueryDto.getPageSize());
        page.setRecords(taskMapper.findByPage(page,qw));
        page.setTotal(taskMapper.selectCount(qw));
        //3.返回
        return page;
    }

    @Override
    @Transactional
    public void updateData(Task task) {
        //获取数据
        String title = task.getTitle();//标题
        String content = task.getContent();//内容
        task.setTime(LocalDateTime.now());
        LocalDateTime time = task.getTime();              //时间
        String publisher = task.getPublisher();         //发布人
        LocalDateTime deadline = task.getDeadline();      //任务结束日期

        List<Long> executiveId = task.getExecutiveId();//执行人id

        Integer taskId = task.getTaskId();//获取该任务id
        //更新其他数据
        taskMapper.updateData(title,content,time,taskId,publisher,deadline);
        //查询执行人数据是否更改过
        List<Long>oldData = taskEmployeeMapper.selectexecutiveIdByTaskId(taskId);
        //如果更改了执行更新数据
        if(!executiveId.equals(oldData)){
            //先删除该任务所对应的多个执行人
            taskEmployeeMapper.deleteByTaskid(taskId);
            //遍历执行人数据
            for (Long employeeId : executiveId) {
                //将任务下的多个执行人更改为当前获取的执行人数据
                taskEmployeeMapper.updateByTaskId(taskId,employeeId);
            }
        }
    }

    @Override
    public List<Task> findall() {
     List<Task> tasks= taskMapper.findall();
        return tasks;
    }
    @Transactional
    @Override
    public void insert(Task task) {
        task.setTime(LocalDateTime.now());
        taskMapper.insert(task);
        Integer taskId = task.getTaskId();
        List<Long> executiveIds = task.getExecutiveId();
        for (Long employeeId : executiveIds) {
            taskEmployeeMapper.updateByTaskId(taskId,employeeId);
        }
    }

    @Override
    public void deleByid(Integer taskId) {

        //先删除中间表的数据
        taskEmployeeMapper.deleteByTaskid(taskId);
        //在删除任务数据
        taskMapper.deleteById(taskId);
    }


}
